package com.example.capsule_chat0322.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.PositionBean;
import com.example.capsule_chat0322.interfaces.NearPeopleItemOnClickInterface;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/1 21:21
 */
public class NearPeopleAdapter extends RecyclerView.Adapter<NearPeopleAdapter.ViewHold> implements View.OnClickListener {

    private List<PositionBean> positionBeanList;
    private NearPeopleItemOnClickInterface nearPeopleItemOnClickInterface;
    private RecyclerView rvParent;

    public void setFunctionItemOnClickInterface(NearPeopleItemOnClickInterface nearPeopleItemOnClickInterface) {
        this.nearPeopleItemOnClickInterface = nearPeopleItemOnClickInterface;
    }

    static class ViewHold extends RecyclerView.ViewHolder {

        LinearLayout chatRecordItem;
        TextView chatRecord;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            chatRecordItem = itemView.findViewById(R.id.chat_record_item);
            chatRecord = itemView.findViewById(R.id.chat_record);
        }
    }


    public NearPeopleAdapter(List<PositionBean> positionBeanList) {
        this.positionBeanList = positionBeanList;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        rvParent = (RecyclerView) parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_record_item, parent, false);
        view.setOnClickListener(this);
        return new NearPeopleAdapter.ViewHold(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, int position) {

        PositionBean positionBean = positionBeanList.get(position);
        holder.chatRecordItem.setVisibility(View.VISIBLE);
        StringBuilder sb = new StringBuilder();
        char[] chars = positionBean.getDistance().toCharArray();
        for (char aChar : chars) {
            if (Character.isLowerCase(aChar) || Character.isUpperCase(aChar)) {
                continue;
            }
            sb.append(aChar);
        }
        String sex = positionBean.getSex() == -1 ? "未知" : (positionBean.getSex() == 0 ? "女" : "男");
        String uploadTime = positionBean.getUploadTime();
        String str = "ID: " + positionBean.getUserId() + "\n"
                + "昵称: " + positionBean.getUserName() + "\n"
                + "性别: " + sex + "\n"
                + "距离您: " + sb.substring(0, sb.lastIndexOf(".")) + "米" + "\n"
                + "更新时间: " + uploadTime;


        holder.chatRecord.setText(str);
    }

    @Override
    public int getItemCount() {
        return positionBeanList.size();
    }


    @Override
    public void onClick(View view) {
        //根据RecyclerView获得当前View的位置
        int position = rvParent.getChildAdapterPosition(view);
        //程序执行到此，会去执行具体实现的onItemClick()方法
        if (nearPeopleItemOnClickInterface != null) {
            nearPeopleItemOnClickInterface.onItemClick(rvParent, view, positionBeanList.get(position).getUserId());
        }
    }


}
